<?php
	/*
	 *模型：user
	 *class.user.php
	 *By QINIAO
	 */
	 
	class user{

		var $db;

		function user($dbhandle){
			$this->db = $dbhandle;
		}
		
		//
		 
		function getAllUser($page = 1, $prePageNum){
			$start_limit = !empty($page) ? ($page - 1) * $prePageNum : 0;
			$limit = $prePageNum ? "LIMIT $start_limit, $prePageNum" : '';
			$users	= $this->db->fetch_all_assoc("select * from ".dbprefix."app_user order by userid desc $limit");
			if($users){
			foreach($users as $item){
				$arrUser[]	= $this->getOneUserByUserid($item['userid']);	
			}}
			
			return $arrUser;
		}
		
		//
		 
		function getUserNum($virtue, $setvirtue){
			$where = 'where '.$virtue.'='.$setvirtue.'';
			$res = $this->db->query("SELECT * FROM ".dbprefix."app_user $where" );
			$userNum = $this->db->num_rows($res);
			return $userNum;
		}
		
		//获取一个用户的信息
		function getOneUserByUserid($userid){
		
			$strUser = $this->db->once_fetch_assoc("select * from ".dbprefix."app_user_info where userid='$userid'");
			
			//头像
			if($strUser['face'] != ''){
				$menu = substr($userid,0,1);
				//找出后缀
				$fileInfo=pathinfo(THINKROOT.'/'.$strUser['face']);
				$extension=$fileInfo['extension'];
				$strUser['bigface'] = 'data/uploadfile/user/'.$menu.'/'.$userid.'/'.$userid.'_98.'.$extension;
				
				if(!is_file(THINKROOT.'/'.$strUser['bigface'])){
					$strUser['bigface'] = 'data/uploadfile/user/'.$menu.'/'.$userid.'/'.$userid.'_96.'.$extension;
				}
				
			}else{
				$strUser['bigface'] = 'data/uploadfile/user/default/n_default.gif';
				$strUser['face']	= 'data/uploadfile/user/default/noavatar.gif';
			}
			
			//同城
			if($strUser['cityid'] != '0'){
				$strUser['city'] = $this->db->once_fetch_assoc("select * from ".dbprefix."app_location_city where cityid='".$strUser['cityid']."'");
			}else{
				$strUser['city']['cityname'] = '火星'; 
			}
			
			$pattern='/(http:\/\/|https:\/\/|ftp:\/\/)([\w:\/\.\?=&-_]+)/is';

			$strUser['signed'] = hview(preg_replace($pattern, '<a rel="nofollow" target="_blank" href="\1\2">\1\2</a>', $strUser['signed']));
			
			return $strUser;
		}
		
		//获取要编辑的用户信息
		function getUserForEdit($userid){
		
			$strUser = $this->db->once_fetch_assoc("select * from ".dbprefix."app_user_info where userid='$userid'");
			
			//头像
			if($strUser['face'] == ''){
				$strUser['face']	= 'data/uploadfile/user/default/noavatar.gif';
			}
			
			//同城
			if($strUser['cityid'] != '0'){
				$strUser['city'] = $this->db->once_fetch_assoc("select * from ".dbprefix."app_location_city where cityid='".$strUser['cityid']."'");
			}else{
				$strUser['city']['cityname'] = '火星'; 
			}
			
			return $strUser;
		}
		
		//获取简单用户信息为其他APP调用
		function getUserForApp($userid){
			$strUser = $this->db->once_fetch_assoc("select userid,username,cityid,face,signed,count_score,count_follow,count_followed from ".dbprefix."app_user_info where userid='$userid'");
			
			//城市数据
			if($strUser['cityid'] != '0'){
				$strUser['city'] = $this->db->once_fetch_assoc("select * from ".dbprefix."app_location_city where cityid='".$strUser['cityid']."'");
			}else{
				$strUser['city']['cityname'] = '火星'; 
			}
			
			//头像
			if($strUser['face']==''){
				$strUser['face'] = 'data/uploadfile/user/default/noavatar.gif';
			}
			//签名 
			$pattern='/(http:\/\/|https:\/\/|ftp:\/\/)([\w:\/\.\?=&-_]+)/is';
			$strUser['signed'] = hview(preg_replace($pattern, '<a rel="nofollow" target="_blank" href="\1\2">\1\2</a>', $strUser['signed']));
			
			return $strUser;
			
		}
		
		//收藏的帖子 
		function getCollectTopic($userid,$limit){
		
			$arrCollect = $this->db->fetch_all_assoc("select * from ".dbprefix."app_group_topics_collects where userid='".$userid."' order by addtime desc limit $limit");
			
			if(is_array($arrCollect)){
				foreach($arrCollect as $item){
					$strTopic = $this->db->once_fetch_assoc("select * from ".dbprefix."app_group_topics where topicid = '".$item['topicid']."'");
					$arrTopic[] = $strTopic;
				}
			}
			
			return $arrTopic;
			
		}
		
		//获取用户积分 
		function getUserScore($userid){
			$strUser = $DB->once_fetch_assoc("select count_score from".dbprefix."app_user_info where userid='$userid'");
			return $strUser['count_score'];
		}
						
	}